<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | Ouath授权控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\OauthService;
use common\service\UserService;
use think\Request;
use think\Response;

class OauthController extends VerifyController
{
    /**
     * 查询应用列表
     * @param Request $request
     * @return \think\Response
     */
    public function selectClient(Request $request)
    {
        try {
            $param = $request->param();
            $oauthService = new OauthService();
            $resData = $oauthService->selectClient($param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加客户端 (同时上传缩略图)
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function addClientWithMedia(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->addClientWithMedia($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改客户端 (同时修改缩略图)
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function modifyClientWithMedia(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->modifyClientWithMedia($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除客户端
     * @param Request $request
     * @return \think\Response
     */
    public function deleteClient(Request $request)
    {
        try {
            $param = $request->param();
            $oauthService = new OauthService();
            $resData = $oauthService->deleteClient($param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 切换授权客户端激活状态
     * @param Request $request
     * @return \think\Response
     */
    public function switchClientActive(Request $request)
    {
        try {
            $param = $request->param();
            $oauthService = new OauthService();
            $resData = $oauthService->switchClientActive($param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 同步用户注册数据
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function syncRegister(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->syncRegister($request->param());
            return json(success_response('Successfully registered user.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送验证短信
     * @param Request $request
     * @return \think\Response
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function sendSMS(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->sendSMS($request->param());
            return json(success_response('Successfully registered user.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 远端验证登陆
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function remoteVerifyLogin(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->remoteVerifyLogin($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送系统通知消息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function sendSystemNoticeMessage(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->sendSystemNoticeMessage($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 渲染消息模板
     * @param Request $request
     * @return \think\Response
     */
    public function renderMessageTemplate(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->renderMsgTemplate($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 创建聊天
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function createChat(Request $request)
    {
        try {
            $param = $request->param();
            $oauthService = new OauthService();
            $resData = $oauthService->createChat($param['data'], $param);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加聊天用户
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function inviteChatUser(Request $request)
    {
        try {
            $param = $request->param();
            $oauthService = new OauthService();
            $resData = $oauthService->inviteChatUser($param['data'], $param);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除聊天用户
     * @param Request $request
     * @return \think\Response
     */
    public function deleteChatUser(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->deleteChatUser($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 聊天会话查询
     * @param Request $request
     * @return \think\Response
     */
    public function findChat(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->findChat($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除聊天
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function deleteChat(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->deleteChat($request->param());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 清除当前用户 指定设备码的登录信息
     * @param Request $request
     * @return \think\Response
     */
    public function clearUserTokenByDeviceCode(Request $request)
    {
        try {
            $param = $request->param();
            $userService = new UserService();
            $resData = $userService->clearTokenByDeviceCode($request->getUserId(), $param['param']['device_unique_code']);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送消息到指定用户的通道
     * @param Request $request
     * @return Response
     */
    public function sendDataToUserChannel(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $resData = $oauthService->sendDataToUserChannel($request->param());
            return json(success_response('OK', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 通过设备码清空token
     * @param Request $request
     * @return int|mixed
     */
    public function clearTokenByDeviceCode(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $res = $oauthService->clearTokenByDeviceCode($request->param()['device_unique_code'], $request->userId);
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改租户框架协议状态
     * @param Request $request
     * @return \think\Response
     */
    public function markTenantFrameworkAgreementStatus(Request $request)
    {
        try {
            $oauthService = new OauthService();
            $res = $oauthService->markTenantFrameworkAgreementStatus($request->param());
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
